//
//  InputCodeViewController.m
//  AppleMarket
//
//  Created by yangy on 24/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "InputCodeViewController.h"

// view
#import "CodeMainView.h"
// viewModel
#import "CodeViewModel.h"

@interface InputCodeViewController()
@property (nonatomic, strong) CodeMainView *mainView;
@property (nonatomic, strong) CodeViewModel *viewModel;
@end

@implementation InputCodeViewController
NSString* GetCodeStr(CodeType type){
    switch (type) {
        case CodeTypeLogin:
            return @"Login";
            break;
        case CodeTypeSignup:
            return @"Signup";
            break;
        case CodeTypeForgotLoginPwd:
            return @"ForgotLoginPwd";
            break;
        case CodeTypeAddWithdrawAddr:
            return @"AddWithdrawAddr";
        case CodeTypeDoAuthSafePwd:
            return @"DoAuthSafePwd";
            break;
        case CodeTypeDoWithdraw:
            return @"DoWithdraw";
            break;
        case CodeTypeDoMutualTransfer:
            return @"DoMutualTransfer";
            break;
        default:
            break;
    }
    return nil;
}
+ (instancetype) inputCodeViewControllerWithCodeType:(CodeType) codeType
{
    InputCodeViewController *VC = [[[self class] alloc] init];
    VC.codeType = codeType;
    return VC;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.mainView];
    [self bindViewModel];
}

- (void)bindViewModel
{
    @weakify(self);
    self.viewModel.codeType = GetCodeStr(self.codeType);
    [self.viewModel.refershCodeCommand execute:GetCodeStr(self.codeType)];
    
    [self.viewModel.closeSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.viewModel.completeSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self dismissViewControllerAnimated:YES completion:^{
            if(self.completeHandler){
                self.completeHandler(self.viewModel.imageModel,self.viewModel.codeStr_);
            }
        }];
    }];
}

- (CodeMainView *)mainView
{
    if(!_mainView){
        _mainView = [[CodeMainView alloc] initWithFrame:self.view.bounds viewModel:self.viewModel];
    }
    return _mainView;
}

- (CodeViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[CodeViewModel alloc] init];
    }
    return _viewModel;
}


@end
